package com.qd.panda.card.third;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.domain.entity.card.third.ThirdPartyDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.card.third.ThirdPartyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 优惠券/第三方/WEB_第三方券商
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/thirdParty/")
@Slf4j
public class ThirdPartyController {
    private final ThirdPartyService thirdPartyService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:thirdParty:save')")
    @PostMapping(value = "/save")
    public ThirdPartyDTO save(@Validated({DefaultGroup.class}) @RequestBody ThirdPartyDTO dto) {
        dto.setAddTime(LocalDateTime.now());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        return thirdPartyService.save(dto);
    }

    /**
     * 修改
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:thirdParty:edit')")
    @PutMapping(value = "/update")
    public ThirdPartyDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody ThirdPartyDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        return thirdPartyService.update(dto);
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:thirdParty:delete')")
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            thirdPartyService.removeById(ids.get(0));
        } else {
            thirdPartyService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ThirdPartyDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return thirdPartyService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ThirdPartyDTO> page(ThirdPartyDTO dto) {
        return thirdPartyService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 全部
     *
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<ThirdPartyDTO> list(ThirdPartyDTO dto) {
        dto = Optional.ofNullable(dto).orElse(new ThirdPartyDTO());
        return thirdPartyService.getListDto(dto);
    }
}